<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/BaseApiController.php';
class area extends  BaseApiController
{
   public function index(){

   }
   
   
   public function list_option(){

   	   $list=$this->db->get("area")->result_array();
   	   $this->WRITE("code",1);
   	   $this->WRITE("list",$list);
   	   $this->RESPONSE();	
   }
   
   public function list_by_page(){
	 $keys=[
		 ["title","option","校区名称",""],
		 ["mobile","option","联系电话",""],
		 ["manager","option","负责人",""],
		 ["address","option","校区地址",""]
	 ];
	 
	 $data=$this->getFields($keys);

	 foreach($data as $key=>$value){
		 $this->db->like($key,$value); 
	 }
	 
	 $res=$this->db->select("count(*) as count")->get("area")->row_array();
	 $numrows=(int)$res["count"];
	 
	 foreach($data as $key=>$value){
		 $this->db->like($key,$value); 
	 }
	 $rows=$this->db->get("area")->result_array();
	 
	 $this->WRITE("code",1);
	 $this->WRITE("numrows",$numrows);
	 $this->WRITE("list",$rows);
	 $this->RESPONSE();	 
   }
   
   public function create(){
	   $keys=[
	   		 ["title","option","校区名称",""],
			  ["desc","option","校区描述",""],
	   		 ["mobile","option","联系电话",""],
	   		 ["manager","option","负责人",""],
	   		 ["address","option","校区地址",""]
	   ];
	   
	   $data=$this->getFields($keys);
	   
	   $res=$this->db->insert("area",$data);
	   if($res){
		   $this->WRITE("code",1);
		   $this->WRITE("msg","新增校区成功!");
		   $this->RESPONSE();	
	   }else{
		   $this->WRITE("code",0);
		   $this->WRITE("msg","新增校区失败!");
		   $this->RESPONSE();	
	   }

   }
   
   public function update(){
   	   $keys=[
		     ["id","required","校区id",""],
   	   		 ["title","option","校区名称",""],
   			  ["desc","option","校区描述",""],
   	   		 ["mobile","option","联系电话",""],
   	   		 ["manager","option","负责人",""],
   	   		 ["address","option","机构地址",""]
   	   ];
   	   
   	   $data=$this->getFields($keys);
   	   
   	   $res=$this->db->where("id",$data["id"])->update("area",$data);
   	   if($res){
   		   $this->WRITE("code",1);
   		   $this->WRITE("msg","更新校区信息成功!");
   		   $this->RESPONSE();	
   	   }else{
   		   $this->WRITE("code",0);
   		   $this->WRITE("msg","更新校区信息失败!");
   		   $this->RESPONSE();	
   	   }
   
   }
   
   
   public function delete(){  	   
   	   $data=$this->getFields(array(["id","required","校区编码",""]));
   	   
   	   $res=$this->db->where("id",$data["id"])->delete("area",$data);
   	   if($res){
   		   $this->WRITE("code",1);
   		   $this->WRITE("msg","删除分校区成功!");
   		   $this->RESPONSE();	
   	   }else{
   		   $this->WRITE("code",0);
   		   $this->WRITE("msg","删除分校区失败!");
   		   $this->RESPONSE();	
   	   }
   
   }
}